/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.Ghe;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;

/**
 *
 * @author PC
 */
public class GheDAO {
     public static boolean Them(Ghe n) {
        try {
            Connection conn = DBConnect.Connect();
            Statement statement = conn.createStatement();
            String sql = String.format ("INSERT INTO ghe(so_hieu, toa, tinh_trang,gia) VALUES('%s','%s','%d','%d')   ,n.SoHieu,n.toa,n.tinhTrang,n.Gia");
            statement.executeUpdate(sql);
            conn.close();
            return true;
           // JOptionPane.showMessageDialog(null, "Thêm thành công!!!", "Thông báo", 1);
        } catch (Exception ex) {
           // JOptionPane.showMessageDialog(null, "Thêm Không thành công!!!", "Thông báo", 1);
        }
        return false;
    }

    public static boolean Xoa(String id) {
        try {
            Connection conn = DBConnect.Connect();
            String strSQL = "delete from Ghe where so_hieu = ?";
            PreparedStatement pst = conn.prepareStatement(strSQL);
            pst.setString(1, id);
            pst.executeUpdate();
            //JOptionPane.showMessageDialog(null, "Xóa thành công!!!", "Thông báo", 1);
            pst.close();
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            ex.getMessage();
           // JOptionPane.showMessageDialog(null, ex.getStackTrace(), "Thông báo lỗi", JOptionPane.OK_OPTION);
        }
        return false;
    }
    public static Ghe Get(String maghe, String toa)
    {
        try
        {
            Connection con = DBConnect.Connect();
            String sql = String.format("Select * From ghe Where so_hieu = '%s' and toa='%s'",maghe,toa);
            PreparedStatement ps = con.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            
            Ghe gh = new Ghe();
            while(rs.next())
            {
                gh.Gia = rs.getFloat("gia");
                gh.SoHieu = rs.getString("so_hieu");
                gh.Toa = rs.getString("toa");
            }
            return gh;
        }
        catch (Exception ex)
        {
        }
        return null;
    }
    public static boolean UpdateTinhTrang(String maghe, String toa,int tinhtrang)
    {
        try
        {
            Connection con = DBConnect.Connect();
            String sql = String.format("Update ghe set tinh_trang= %s where so_hieu='%s' and toa='%s' ",tinhtrang,maghe,toa);
            PreparedStatement ps = con.prepareStatement(sql);
            int n = ps.executeUpdate();

        }
        catch (Exception ex)
        {
        }
        return false;
    }
    public static ResultSet TimKiemTatCa() {
        try {
            Connection conn = DBConnect.Connect();
            String sql = "Select ghe.SO_HIEU,toa.SO_HIEU,tt.TEN,GHE.GIA  from TOA toa, GHE ghe, TINH_TRANG_GHE tt where ghe.TOA=toa.SO_HIEU and ghe.TINH_TRANG=tt.ID";
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery(sql);
          //  conn.close();
            return rs;

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex.getStackTrace(), "Thông báo lỗi", JOptionPane.OK_OPTION);
            return null;
        }

    }
      public static boolean KiemTraTonTai(String MaNV)
    {
        try
        {
            Connection con = DBConnect.Connect();
            String sql = "Select * From ghe Where MaNV like ?";
            
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1, MaNV);
            
            ResultSet rs = ps.executeQuery();
            
            if (rs.next())
                return true;
        }
        catch (Exception ex)
        {
        }
        return false;
    }
      
      
      public static ResultSet TimGheCuaToa(String MaToa)
    {
        try
        {
            Connection con = DBConnect.Connect();
            String sql = String.format("Select * From ghe Where toa ='%s'",MaToa);
            
            PreparedStatement ps = con.prepareStatement(sql);            
            ResultSet rs = ps.executeQuery();
            
           return rs;
        }
        catch (Exception ex)
        {
           return null;
        }
      
    }
       public static boolean Update(Ghe NV,String id)
    {
        try
        {
            Connection con = DBConnect.Connect();
            String sql = "Update ghe set toa=?, tinh_trang=? ,gia = ?, so_hieu=? Where so_hieu = ?";           
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1, NV.Toa);
            ps.setInt(2, NV.TinhTrang);
            ps.setDouble(3, NV.Gia);
            ps.setString(4, NV.SoHieu);
            ps.setString(5,id);
            int n = ps.executeUpdate();
            con.close();
            
            if (n == 1)
                return true;
        }
        catch (Exception ex)
        {
        }
        return false;
    }
}
